package operation;

import Medicine.Pharmacy;
import org.apache.commons.dbutils.QueryRunner;
import JDBC.*;
import org.apache.commons.dbutils.handlers.MapListHandler;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
//添加药品
public class AddEquipment implements IOPeration{
    @Override
    public void work(Pharmacy pharmacy) throws SQLException {
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        System.out.println(" 添加入库器械.... ");

        /*if (isFull(pharmacy)){
            System.out.println("药房拥挤,暂时无法添加药品！！");
            return;
        }*/

        Scanner sc = new Scanner(System.in);
        System.out.println(" 请输入编号:");
        int id = sc.nextInt();

        System.out.println("请输入您想入库的器械名称:");
        String name = sc.next();
        sc.nextLine();

        String query = "SELECT Name FROM Equipment";
        List<Map<String, Object>> equipmentList = qr.query(query, new MapListHandler());
        for (Map<String, Object> equipmentMap : equipmentList) {
            if (equipmentMap.get("name").equals(name)) {
                System.out.println("该器械已入库,无需再添加");
                return;
            }
        }
        //System.out.println("请输入该药品的类型: [医疗器械]");
        String type = "医疗器械";

        System.out.println("请输入该器械的价格:");
        int price = sc.nextInt();
        sc.nextLine();
        System.out.println("请输入入库器械的数量");
        int amount = sc.nextInt();
        //sc.nextLine();

        if(type.equals("医疗器械")) {

            //定义插入药房的药
            String sql = "INSERT INTO equipment (Id,Name,Type,Price,Amount)" +
                    " VALUES(?,?,?,?,?)";
            //设置传入SQL中的参数值
            Object[] parms={id,name,type,price,amount};
            //执行插入信息的SQL
            int count = qr.update(sql, parms);
            if(count<1){
                System.out.println("新增医疗器械信息失败！");
            }else {
                System.out.println("新增医疗器械信息成功！");
            }
        }else {
            System.out.println("该类型暂未收录,导致入库失败");
            return;
        }


    }
}

//还未收录该药品，可以入库药房
//pharmacy.setMedicines(currSize,equipment);
//pharmacy.setUsedSize(currSize+1);
/*Equipment equipment = new Equipment(id,name,type,price,amount);
            int currSize = pharmacy.getUsedSize();
            for (int i = 0; i < currSize; i++) {
                Goods temp = pharmacy.getMedicines(i);
                if (temp.getName().equals(name)){
                    System.out.println("这类药材已经入库，无需再添加");
                    return;
                }
            }*/
    /*private boolean isFull(Pharmacy pharmacy){
        return pharmacy.getGoods().length == pharmacy.getUsedSize();
    }*/

